<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="10" :lg="11" :md="12" :sm="24">
            <a-form-item :label="$t('业务时间')">
              <j-date
                :show-time="{ defaultValue: moment('00:00:00', 'HH:mm:ss') }"
                valueFormat="YYYY-MM-DD HH:mm:ss"
                dateFormat="YYYY-MM-DD HH:mm:ss"
                :placeholder="$t('请选择开始时间')"
                class="query-group-cust"
                v-model="queryParam.createTime_begin"
              ></j-date>
              <span class="query-group-split-cust"></span>
              <j-date
                :show-time="{ defaultValue: moment('23:59:59', 'HH:mm:ss') }"
                valueFormat="YYYY-MM-DD HH:mm:ss"
                dateFormat="YYYY-MM-DD HH:mm:ss"
                :placeholder="$t('请选择结束时间')"
                class="query-group-cust"
                v-model="queryParam.createTime_end"
              ></j-date>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item :label="$t('订单号')">
              <a-input :placeholder="$t('请输入订单号')" v-model="queryParam.orderId"></a-input>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item :label="$t('商品')">
                <j-search-select-tag
                  :placeholder="$t('请选择商品')"
                  v-model="queryParam.goodsId"
                  dict="bis_goods_infor,name,id"
                />
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item :label="$t('操作员')">
                <a-input :placeholder="$t('请输入操作员')" v-model="queryParam.createBy"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="5" :lg="5" :md="6" :sm="24" v-if="!tenantid">
              <a-form-item :label="$t('门店')">
                <a-select v-model="queryParam.tenantId">
                  <a-select-option v-for="(item, i) in shops" :key="i" :value="item.value"
                    >{{ item.title
                    }}<span v-if="item.value != '' && item.value != '0'">（{{ item.value }}）</span></a-select-option
                  >
                </a-select>
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">{{ $t('查询') }}</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">{{
                $t('重置')
              }}</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? $t('收起') : $t('展开') }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'" />
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button type="primary" :loading="exloading" @click="handleExportXls($t('商品调拨明细'))" v-has="'BisGoodsTransferDetailsController:export'">{{
        $t('导出')
      }}</a-button>
      <!-- <a-button type="primary" v-print="'#printme'">{{ $t('打印') }}</a-button> -->
    </div>

    <!-- table区域-begin -->
    <div id="printme">
      <a-table
        ref="table"
        size="middle"
        :scroll="{ x: true }"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        class="j-table-force-nowrap"
        @change="handleTableChange"
      >
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">{{ $t('无图片') }}</span>
          <img
            v-else
            :src="getImgView(text)"
            height="25px"
            alt=""
            style="max-width: 80px; font-size: 12px; font-style: italic"
          />
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">{{ $t('无文件') }}</span>
          <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">
            {{ $t('下载') }}
          </a-button>
        </template>
        <span slot="action" slot-scope="text, record">
          <a @click="handleDetail(record)">{{ $t('详情') }}</a>
        </span>
        <template slot="footer">
          <div style="font-weight: bold">
            <span>{{ $t('数量') }}：{{ totalInfo.number?totalInfo.number.toFixed(0):0 }}</span>
            <span style="margin-left: 16px">{{ $t('总金额') }}：{{ totalInfo.totalAmount?totalInfo.totalAmount.toFixed(2):'0.00' }}</span>
          </div>
        </template>
      </a-table>
    </div>

    <bis-goods-transfer-details-modal ref="modalForm" @ok="modalFormOk"></bis-goods-transfer-details-modal>
  </a-card>
</template>

<script>
import moment from 'moment'
import '@/assets/less/TableExpand.less'
import Vue from 'vue'
import { TENANT_ID } from '@/store/mutation-types'
import { initDictOptions } from '@/components/dict/JDictSelectUtil'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import BisGoodsTransferDetailsModal from './modules/BisGoodsTransferDetailsModal'

export default {
  name: 'BisGoodsTransferDetailsList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    BisGoodsTransferDetailsModal,
  },
  data() {
    return {
      description: '商品调拨明细管理页面',
      moment,
      // 表头
      columns: [
        {
          title: this.$t('订单号'),
          align: 'center',
          dataIndex: 'orderId',
        },
        {
          title: this.$t('业务时间'),
          align: 'center',
          dataIndex: 'createTime',
        },
        {
          title: this.$t('商品编码'),
          align: 'center',
          dataIndex: 'goodsCode',
        },
        {
          title: this.$t('商品名称'),
          align: 'center',
          dataIndex: 'goodsId_dictText',
        },
        {
          title: this.$t('成本'),
          align: 'right',
          dataIndex: 'unitPrice',
          customRender: (text, r) => {
            return text.toFixed(2);
          }
        },
        {
          title: this.$t('调拨数量'),
          align: 'right',
          dataIndex: 'number',
          customRender: (text, r) => {
            return text.toFixed(0);
          }
        },
        {
          title: this.$t('总金额'),
          align: 'right',
          dataIndex: 'totalAmount',
          customRender: (text, r) => {
            return text.toFixed(2);
          }
        },
        {
          title: this.$t('备注'),
          align: 'center',
          dataIndex: 'comment',
        },
        {
          title: this.$t('操作员'),
          align: 'center',
          dataIndex: 'createBy',
        },
      ],
      url: {
        list: '/goods/bisGoodsTransferDetails/list',
        delete: '/goods/bisGoodsTransferDetails/delete',
        deleteBatch: '/goods/bisGoodsTransferDetails/deleteBatch',
        exportXlsUrl: '/goods/bisGoodsTransferDetails/exportXls',
        importExcelUrl: 'goods/bisGoodsTransferDetails/importExcel',
      },
      dictOptions: {},
      superFieldList: [],
      totalCallback: this.totalCallbacks,
      tenantid: Vue.ls.get(TENANT_ID),
      shops: [],
      queryParam: {
        tenantId: '',
      },
    }
  },
  created() {
    this.getSuperFieldList()
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
  },
  methods: {
    totalCallbacks(totalInfo) {
      this.totalInfo = totalInfo
    },
    searchReset() {
      this.queryParam = {
        tenantId: '',
      }
      this.searchQuery()
    },
    initDictConfig() {
      if (!this.tenantid) {
        initDictOptions('bis_shop,name,code').then((res) => {
          if (res.success) {
            this.shops = res.result
            this.shops.unshift({ value: '0', title: this.$t('总部') })
            this.shops.unshift({ value: '', title: this.$t('全部') })
          }
        })
        this.columns.push({
          title: this.$t('所属门店'),
          align: 'left',
          dataIndex: 'tenantId',
          customRender: (t, r) => {
            let str
            if (!t) {
              str = this.$t('总部')
            } else {
              str = r.tenantId_dictText ? t + ' ' + r.tenantId_dictText : t
            }
            return str
          },
        })
      }
    },
    getSuperFieldList() {
      let fieldList = []
      fieldList.push({ type: 'string', value: 'orderId', text: this.$t('订单号'), dictCode: '' })
      fieldList.push({
        type: 'sel_search',
        value: 'goodsId',
        text: this.$t('卖品名称'),
        dictTable: 'bis_goods_infor',
        dictText: 'name',
        dictCode: 'id',
      })
      fieldList.push({ type: 'int', value: 'number', text: this.$t('调拨数量'), dictCode: '' })
      fieldList.push({ type: 'string', value: 'comment', text: this.$t('备注'), dictCode: '' })
      this.superFieldList = fieldList
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>